//
//  AppDelegate.swift
//  WBDemo
//
//  Created by Meng on 16/12/14.
//  Copyright © 2016年 demo. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        
        window = UIWindow(frame: UIScreen.main.bounds)
        
        window?.backgroundColor = UIColor.white
//        let main = MainViewController()
//        let sb = UIStoryboard(name: "NewFeature", bundle: nil)
//        let main = sb.instantiateInitialViewController()
        
        window?.rootViewController = defaultRootViewController()
        window?.makeKeyAndVisible()
        
        UINavigationBar.appearance().tintColor = UIColor.orange
        UITabBar.appearance().tintColor = UIColor.orange
        
        MYLog(NSHomeDirectory())
        
        NotificationCenter.default.addObserver(self, selector: #selector(changeRootViewController(noti:)), name: NSNotification.Name(rawValue: SwitchRootViewController), object: nil)
        
        return true
    }
    
    private func defaultRootViewController() -> UIViewController {
        
        if UserAccount.isLogin() { // 已登录
            if isNewVersion() { // 有新版本
                return UIStoryboard(name: "NewFeature", bundle: nil).instantiateInitialViewController()!
            } else { // 无新版本进入欢迎界面
                return UIStoryboard(name: "Welcome", bundle: nil).instantiateInitialViewController()!
            }
        } else { // 没有登录
            return UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController()!
        }
    }
    
    @objc private func changeRootViewController(noti: Notification) {
        if noti.object as! Bool {
            window?.rootViewController = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController()
        } else {
            window?.rootViewController = UIStoryboard(name: "Welcome", bundle: nil).instantiateInitialViewController()
        }
    }
    
    // 是否有新版本
    private func isNewVersion() -> Bool{
        
        let userDefaultsKey = "wbdemoVersion"
        
        // 获取当前版本号
        let currentVersion = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String
        // 之前版本号
        let oldVersion = UserDefaults.standard.object(forKey: userDefaultsKey) as? String ?? "0.0"
        // 比较版本号
        // 1.0 0.0
        if currentVersion.compare(oldVersion) == ComparisonResult.orderedDescending {
            UserDefaults.standard.set(currentVersion, forKey: userDefaultsKey)
            UserDefaults.standard.synchronize() // iOS7以前需要写, iOS7以后不用写
            return true
        }
        return false
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }

}

func MYLog<T>(_ message: T, fileName: String = #file, methodName: String = #function, lineNumber: Int = #line) {
    #if DEBUG
    print("\((fileName as NSString).pathComponents.last!).\(methodName)[Line \(lineNumber)] \(message)")
    #endif
}

